package com.sais.manage.boot.building.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sais.common.entity.Result;
import com.sais.manage.boot.building.dto.BatchRoomUser;
import com.sais.manage.boot.building.dto.RoomBatchSaveDTO;
import com.sais.manage.boot.building.dto.RoomDTO;
import com.sais.manage.boot.building.dto.RoomSaveDTO;
import com.sais.manage.boot.building.dto.RoomSearchDTO;
import com.sais.manage.boot.building.dto.UserRoomDTO;
import com.sais.manage.boot.building.service.IRoomService;
import com.sais.manage.boot.platform.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 宿舍管理系统-房间 controller 类
 *
 * @author zwp
 * @date 2020/1/30
 **/
@Api(tags = "房间管理")
@Slf4j
@CrossOrigin
@Validated
@RestController
@RequestMapping("/api/distribution/room")
public class RoomController {

    @Resource
    private IRoomService roomService;

    @ApiOperation("房间 保存")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result saveRoom(@RequestBody RoomSaveDTO roomSaveDTO) {
        roomService.saveRoom(roomSaveDTO);
        return new Result();
    }

    @ApiOperation("房间 删除")
    @RequestMapping(value = "/delete/{roomId}", method = RequestMethod.GET)
    public Result deleteRoom(@PathVariable("roomId") String roomId) {
        roomService.deleteRoom(roomId);
        return new Result();
    }

    @ApiOperation("根据id 查询")
    @RequestMapping(value = "/select/{roomId}", method = RequestMethod.GET)
    public Result selectRoom(@PathVariable("roomId") String roomId) {
        RoomDTO room = roomService.selectRoom(roomId);
        return new Result().data("room", room);
    }

    @ApiOperation("根据宿舍楼 查询房间集合")
    @RequestMapping(value = "/list/building/{buildingId}", method = RequestMethod.GET)
    public Result listRoomByBuildingId(@PathVariable("buildingId") String buildingId) {
        List<RoomDTO> rooms = roomService.listRoomByBuildingId(buildingId);
        return new Result().data("rooms", rooms);
    }

    @ApiOperation("分页条件 查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result listRoom(@RequestParam(defaultValue = "1") Integer current,
                           @RequestParam(defaultValue = "30") Integer size,
                           RoomSearchDTO searchDTO) {
        Page<RoomDTO> page = roomService.listRoom(current, size, searchDTO);
        return new Result().data("rooms", page);
    }

    @ApiOperation("批量创建房间")
    @RequestMapping(value = "/batch/create", method = RequestMethod.POST)
    public Result batchSaveRoom(@Valid @RequestBody RoomBatchSaveDTO roomBatchSaveDTO) {
        roomService.batchSaveRoom(roomBatchSaveDTO);
        return new Result();
    }

    @ApiOperation("根据学生id 查询房间信息")
    @RequestMapping(value = "/select/user")
    public Result selectRoomByUserId() {
        String userId = ShiroUtils.getLoginUser().getUkId();
        UserRoomDTO room = roomService.selectRoomByUserId(userId);
        return new Result().data("room", room);
    }

    @ApiOperation("批量绑定房间用户")
    @RequestMapping(value = "/batch/bind/user", method = RequestMethod.POST)
    public Result batchBindRoomUser(@RequestBody BatchRoomUser batchRoomUser) {
        roomService.batchBindRoomUser(batchRoomUser);
        return new Result();
    }

    @ApiOperation("批量删除用户")
    @RequestMapping(value = "/batch/unBind", method = RequestMethod.POST)
    public Result batchUnBindRoomUser(@RequestBody BatchRoomUser batchRoomUser){
        roomService.batchUnBindRoomUser(batchRoomUser.getUsers());
        return Result.response();
    }

}
